package com.hiultra.archive.service.impl;

import com.hiultra.archive.domain.*;
import com.hiultra.archive.mapper.*;
import com.hiultra.archive.service.IElecPatchService;
import com.hiultra.common.constant.BusinessConstants;
import com.hiultra.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service
public class ExaminePatchServiceImpl implements IElecPatchService {

    @Autowired
    private SimfExamineItemMapper itemMapper;

    @Autowired
    private SimfExamineSetupMapper setupMapper;

    @Autowired
    private ArcBorrowMapper arcBorrowMapper;

    @Autowired
    private SysExamineConfigMapper sysExamineConfigMapper;

    @Autowired
    private ArcAppraisalMapper arcAppraisalMapper;

    @Autowired
    private ArcDestroyMapper arcDestroyMapper;


    /**
     * 提交审批
     */
    @Override
    public int batchToExamine(String examineType, String pks) {
        // TODO Auto-generated method stub
        List<String> pkli = Arrays.asList(pks.split(","));

        int i = 0;
        for (String temps : pkli) {
                ArcBorrow tempObj = new ArcBorrow();
                tempObj.setBorrowNumber(temps);
                tempObj.setStatus(Integer.valueOf(1));
                ArcBorrow tempBill = arcBorrowMapper.selectArcBorrowByBorrowId(Long.parseLong(temps));
                if (tempBill.getStatus() == 0) {
                    arcBorrowMapper.updateArcBorrow(tempObj);
                    insert(examineType, temps);
                }
                i++;
        }
        return i;
    }

    /**
     * 添加
     */
    @Transactional
    public int insert(String examineType, String billNumber) {

        SimfExamineItem item = new SimfExamineItem();
        item.setBillNumber(billNumber);
        //根据单据类型查询单据
        SysExamineConfig sysExamineConfig = sysExamineConfigMapper.selectSysExamineConfigByExamineConfigOrderType(examineType);
        if (null == sysExamineConfig) {
            throw new ServiceException("未查询到该类型单据审批配置信息，请检查后重试！");
        }
        if (null == sysExamineConfig.getExamineConfigId()) {
            throw new ServiceException("未配置单据审批流程，请检查后重试！");
        }
        item.setExamineId(sysExamineConfig.getExamineId());

        List<SimfExamineSetup> setupLi = setupMapper.selectSimfExamineSetupList(new SimfExamineSetup() {{
            setExamineId(sysExamineConfig.getExamineId());
            setSortNo(Integer.valueOf(1));
        }});
        if (setupLi.size() <= 0) {
            throw new ServiceException("未查询到审批步骤信息，请联系系统管理员！");
        }
        SimfExamineSetup setup = setupLi.get(0);
        item.setExamineSetupId(setup.getSetupId());
        item.setExamineId(setup.getExamineId());
        item.setExamineUserId(setup.getUserId());
        item.setStatus("0");
        item.setExamineResult("0");
        item.setInsertTime(new Date());

        int i = itemMapper.insertSimfExamineItem(item);
        return i;
    }

    @Override
    @Transactional
    public int approved(SimfExamineItem entity) {

        //设置下一步骤
        entity.setExamineResult("1");
        SimfExamineItem tempItem = itemMapper.selectSimfExamineItemByItemId(entity.getItemId());
        SimfExamineSetup tempSetup = setupMapper.selectSimfExamineSetupBySetupId(tempItem.getExamineSetupId());
        int tempSortNo = tempSetup.getSortNo();

        SimfExamineSetup newSetup = setupMapper.selectSimfExamineSetupByOne(new SimfExamineSetup() {{
            setSetupId(Long.valueOf(tempSortNo + 1));
            setExamineId(tempSetup.getExamineId());
        }});
        if (null != newSetup) {
            //下一步骤不为空进入下一步骤
            SimfExamineItem item = new SimfExamineItem();
            item.setBillNumber(entity.getBillNumber());
            item.setExamineId(entity.getExamineId());
            item.setExamineSetupId(newSetup.getSetupId());
            item.setExamineUserId(newSetup.getUserId());
            item.setExamineDeptId(newSetup.getDeptId());
            item.setStatus("0");
            item.setExamineResult("0");
            item.setInsertTime(new Date());
            itemMapper.insertSimfExamineItem(item);
        }
        int i = itemMapper.updateSimfExamineItem(entity);
        return i;
    }

    @Override
    @Transactional
    public int failed(SimfExamineItem entity, String examineType) {
        // TODO Auto-generated method stub
        //修改当前项目
        //回退上一步骤，如果是第一步审批，修改单据为新建状态
        entity.setExamineResult("2");
        SimfExamineItem tempItem = itemMapper.selectSimfExamineItemByItemId(entity.getItemId());
        SimfExamineSetup tempSetup = setupMapper.selectSimfExamineSetupBySetupId(tempItem.getExamineSetupId());

        int tempSortNo = tempSetup.getSortNo();

        SimfExamineSetup newSetup = setupMapper.selectSimfExamineSetupByOne(new SimfExamineSetup() {{
            setSetupId(Long.valueOf(tempSortNo - 1));
            setExamineId(tempSetup.getExamineId());
        }});
        if (null != newSetup) {
            //下一步骤不为空进入下一步骤
            SimfExamineItem item = new SimfExamineItem();
            item.setBillNumber(entity.getBillNumber());
            item.setExamineId(entity.getExamineId());
            item.setExamineSetupId(newSetup.getSetupId());
            item.setExamineUserId(newSetup.getUserId());
            item.setExamineDeptId(newSetup.getDeptId());
            item.setStatus("0");
            item.setExamineResult("0");
            item.setInsertTime(new Date());
            itemMapper.insertSimfExamineItem(item);
        } else {
            //没有上不步，修改单据为新建状态
            updateBill(entity, examineType);
        }
        int i = itemMapper.updateSimfExamineItem(entity);
        return i;
    }

    public void updateBill(SimfExamineItem entity, String examineType) {
        if (BusinessConstants.BORRROW_TYPE.equals(examineType)) {
            //领用单据
            ArcBorrow borrowBill = new ArcBorrow();
            borrowBill.setBorrowNumber(entity.getBillNumber());
            borrowBill.setStatus(Integer.valueOf(0));
            arcBorrowMapper.updateArcBorrow(borrowBill);
        } else if (BusinessConstants.APPRAISAL_TYPE.equals(examineType)) {
            //鉴定单据
            ArcAppraisal appraisalBill = new ArcAppraisal();
            appraisalBill.setAppraisalNo(entity.getBillNumber());
            appraisalBill.setStatus("01");
            arcAppraisalMapper.updateArcAppraisal(appraisalBill);
        } else if (BusinessConstants.DESTROY_TYPE.equals(examineType)) {
            ArcDestroy destroyBill = new ArcDestroy();
            destroyBill.setDestroyNo(entity.getBillNumber());
            destroyBill.setStatus("01");
            arcDestroyMapper.updateArcDestroy(destroyBill);
        }
    }
}
